package com.print;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JApplet;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.codehaus.jackson.map.ObjectMapper;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.model.dj.PrintJLB;
import com.model.dj.PrintJYD;
import com.swetake.util.Qrcode;
import com.util.JackUtil;

/**
 * 客户端标签打印
 * 
 * @author 
 * 
 */
public class LabelApplet4DJ extends JApplet {

	private static final long serialVersionUID = -6391368515081034834L;

	// box
	public void PrintLabelForBox(final String boxId) {
		AccessController.doPrivileged(new PrivilegedAction<Object>() {
			@Override
			public Object run() {

				System.out.println("Start2");
				
				ActiveXComponent axc = new ActiveXComponent("pt1.ct20");
		
					Dispatch dispatch = axc.getObject();
					Variant v = Dispatch.call(dispatch, "ct20", boxId);

				return null;
			}
		});
	}
	
	/**
	 * for 减薄镀膜出货检验单
	 * @param printer
	 * @param str
	 */
	public void Print1(final String printer ,final String str) {
		try {
			PrintJYD printData = new PrintJYD();
			System.out.println(str);
			printData = JackUtil.fromJson(str, printData);
			
			String printTyp = printData.getPrintTyp();
			double stdQty = printData.getStdQty();
			String cusId = printData.getCusId();
			String soId = printData.getSoId();
			String boxId = printData.getBoxId();
			String cusMtrlId = printData.getCusMtrlId();
			String glassFrom = printData.getGlassFrom();
			String mdlId = printData.getMdlId();
			String modelCnt = printData.getModelCnt();
			List<String> defCntAry = printData.getDefCntAry();
			List<String> prdIdAry = printData.getPrdAry();
			int prdCnt = (int) (stdQty > 72 ? 72 : stdQty);
			int totalPrdCnt = prdCnt * Integer.valueOf(modelCnt);//总数量
			
			PrintDJ excel = new PrintDJ("1",1,true);
			//根据excel模板，新建workbook 
			HSSFWorkbook workbook = excel.creatWorkbook();
			
	        //读取了workbook内sheet内容  
			HSSFSheet sheet = workbook.getSheet("sheet1"); 
			
			//第一行
	        HSSFCell cell = sheet.getRow(3).getCell(9);
	        cell.setCellValue(cusId);
	        cell = sheet.getRow(3).getCell(13);
	        cell.setCellValue(soId);
	        cell = sheet.getRow(3).getCell(18);
	        cell.setCellValue(boxId);
	        
            //第二行
	        cell = sheet.getRow(4).getCell(2);
	        cell.setCellValue(cusMtrlId);
	        cell = sheet.getRow(4).getCell(8);
	        cell.setCellValue(glassFrom);
	        cell = sheet.getRow(4).getCell(14);
	        cell.setCellValue(mdlId);
	        cell = sheet.getRow(4).getCell(18);
	        cell.setCellValue(modelCnt + "*" + prdCnt + "=" + totalPrdCnt + "CELL");
	        
	        double def_cnt = 0;
	        double dm_def_cnt = 0;
	        
	        //第三行
	        if(!"C".equals(printTyp)){
	        	cell = sheet.getRow(7).getCell(2);
		        cell.setCellValue(defCntAry.get(0));
		        
		        cell = sheet.getRow(7).getCell(3);
		        cell.setCellValue(defCntAry.get(1));
		        
		        cell = sheet.getRow(7).getCell(4);
		        cell.setCellValue(defCntAry.get(2));
		        
		        cell = sheet.getRow(7).getCell(5);
		        cell.setCellValue(defCntAry.get(3));
		        
		        cell = sheet.getRow(7).getCell(6);
		        cell.setCellValue(defCntAry.get(4));
		        
		        dm_def_cnt = dm_def_cnt + Double.valueOf(defCntAry.get(0))+Double.valueOf(defCntAry.get(1))
    			+Double.valueOf(defCntAry.get(2))+Double.valueOf(defCntAry.get(3))
    			+Double.valueOf(defCntAry.get(4));
		        
		        //镀膜制程良率
	        	double dmzc_yield_1 = (1-dm_def_cnt/totalPrdCnt)*100;
	    		BigDecimal dmzc_p = new BigDecimal(dmzc_yield_1);  
	    		double dmzc_yield = dmzc_p.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		        cell = sheet.getRow(7).getCell(19);
		        cell.setCellValue(dmzc_yield+"%");
	        }
	        
	        cell = sheet.getRow(7).getCell(7);
	        cell.setCellValue(defCntAry.get(5));
	        
	        cell = sheet.getRow(7).getCell(8);
	        cell.setCellValue(defCntAry.get(6));
	        
	        cell = sheet.getRow(7).getCell(9);
	        cell.setCellValue(defCntAry.get(7));
	        
	        cell = sheet.getRow(7).getCell(10);
	        cell.setCellValue(defCntAry.get(8));
	        
	        cell = sheet.getRow(7).getCell(11);
	        cell.setCellValue(defCntAry.get(9));
	        
	        cell = sheet.getRow(7).getCell(12);
	        cell.setCellValue(defCntAry.get(10));
	        
	        cell = sheet.getRow(7).getCell(13);
	        cell.setCellValue(defCntAry.get(11));
	        
	        cell = sheet.getRow(7).getCell(14);
	        cell.setCellValue(defCntAry.get(12));
	        
	        cell = sheet.getRow(7).getCell(15);
	        cell.setCellValue(defCntAry.get(13));
	        
	        cell = sheet.getRow(7).getCell(16);
	        cell.setCellValue(defCntAry.get(14));
	        
	        cell = sheet.getRow(7).getCell(17);
	        cell.setCellValue(defCntAry.get(15));
	        
	        def_cnt = dm_def_cnt + Double.valueOf(defCntAry.get(5))
				+Double.valueOf(defCntAry.get(6))+Double.valueOf(defCntAry.get(7))
				+Double.valueOf(defCntAry.get(8))+Double.valueOf(defCntAry.get(9))
				+Double.valueOf(defCntAry.get(10))+Double.valueOf(defCntAry.get(11))
				+Double.valueOf(defCntAry.get(12))+Double.valueOf(defCntAry.get(13))
				+Double.valueOf(defCntAry.get(14));
	        
	        cell = sheet.getRow(7).getCell(18);
	        cell.setCellValue(def_cnt);
	        
	        //总良率
	        double total_yield_1 = (1-def_cnt/totalPrdCnt)*100;
    		BigDecimal total_p = new BigDecimal(total_yield_1);  
    		double total_yield = total_p.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	        cell = sheet.getRow(7).getCell(20);
	        cell.setCellValue(total_yield+"%");
	        
	        int j=0;
			for(int i=0; i<prdCnt; i++){
				if(i%9==0){
					j++;
				}
				if(i<prdCnt){
					cell = sheet.getRow(8+j).getCell(0);
					cell.setCellValue(prdIdAry.get(i));
					i++;
				}

				if(i<prdCnt){
					cell = sheet.getRow(8+j).getCell(2);
					cell.setCellValue(prdIdAry.get(i));
					i++;
				}

				if(i<prdCnt){
					cell = sheet.getRow(8+j).getCell(4);
					cell.setCellValue(prdIdAry.get(i));
					i++;
				}

				if(i<prdCnt){
					cell = sheet.getRow(8+j).getCell(7);
					cell.setCellValue(prdIdAry.get(i));
					i++;
				}

				if(i<prdCnt){
					cell = sheet.getRow(8+j).getCell(9);
					cell.setCellValue(prdIdAry.get(i));
					i++;
				}
				
				if(i<prdCnt){
					cell = sheet.getRow(8+j).getCell(11);
					cell.setCellValue(prdIdAry.get(i));
					i++;
				}
				
				if(i<prdCnt){
					cell = sheet.getRow(8+j).getCell(14);
					cell.setCellValue(prdIdAry.get(i));
					i++;
				}
				
				if(i<prdCnt){
					cell = sheet.getRow(8+j).getCell(16);
					cell.setCellValue(prdIdAry.get(i));
					i++;
				}
				
				if(i<prdCnt){
					cell = sheet.getRow(8+j).getCell(18);
					cell.setCellValue(prdIdAry.get(i));
				}
			}
			
			FileOutputStream fileOut = new FileOutputStream("C:/temp.xls");  
			workbook.write(fileOut); 
			fileOut.flush();
			fileOut.close(); 
	        excel.printExcel(printer,"C:/temp.xls");   
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println(ex.toString());
		}
	}
	/**
	 * 产品信息记录表
	 * @param printer
	 * @param str
	 */
	public void Print2(final String printer ,final String str) {
		try {
			PrintJLB printData = new PrintJLB();
			System.out.println(str);
			printData = JackUtil.fromJson(str, printData);
			
			double stdQty = printData.getStdQty();
			List<String> prdIdAry = printData.getPrdAry();
			int prdCnt = (int) (stdQty > 40 ? 40 : stdQty);
			
			PrintDJ excel = new PrintDJ("2",1,true);
			//根据excel模板，新建workbook 
			HSSFWorkbook workbook = excel.creatWorkbook();
			
	        //读取了workbook内sheet内容  
			HSSFSheet sheet = workbook.getSheet("sheet1"); 
			
			for(int i=0; i<prdCnt; i++){
				HSSFCell cell = sheet.getRow(7+i).getCell(1);
				cell.setCellValue(prdIdAry.get(i));
			}
			
			FileOutputStream fileOut = new FileOutputStream("C:/temp.xls");  
			workbook.write(fileOut); 
			fileOut.flush();
			fileOut.close(); 
	        excel.printExcel(printer,"C:/temp.xls");   
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println(ex.toString());
		}
	}
	
	//jdf
	public void PrintDJ1(final String printer, final String client, final String model, final String sht_cnt,
            final String total_cnt, final String date, final String productCate,final String size, final String order,
            final String box_id,final String jsonAry,final String wo_id) {
		AccessController.doPrivileged(new PrivilegedAction<Object>() {
			@Override
			public Object run() {
				ObjectMapper mapper = new ObjectMapper();
				String[] ja = null;
				try {
					ja = mapper.readValue(jsonAry, String[].class);
				} catch (Exception e) {
					System.out.println("Error took place");
					e.printStackTrace();
				}
				String sht = "";
				for(int i=0;i<40;i++){
					sht = sht + ja[i] + ",";
				}
				System.out.println("Start2");
				ActiveXComponent axc = new ActiveXComponent("pt1.ct37");
		
					Dispatch dispatch = axc.getObject();
					Variant v = Dispatch.call(dispatch, "ct37", printer, client,model,sht_cnt,total_cnt,date,productCate,size,order,box_id,sht,wo_id);

				return null;
			}
		});
	}
	
	//pallet
	public void PrintDJPallet(final String printer, final String model1, final String qty, final String yield,
            final String date, final String de, final String palletID,final String qa, final String mtrlID,
            final String model2) {
		AccessController.doPrivileged(new PrivilegedAction<Object>() {
			@Override
			public Object run() {
				System.out.println("Start2");
				ActiveXComponent axc = new ActiveXComponent("pt1.ct38");
		
					Dispatch dispatch = axc.getObject();
					Variant v = Dispatch.call(dispatch, "ct38", printer, model1,qty,yield,date,de,palletID,qa,mtrlID,model2);

				return null;
			}
		});
	}
	//qrcode picture
			public void PrintQR(final String printer, final String str) {
			
				Qrcode rcode = new Qrcode();
				rcode.setQrcodeVersion(3);
				byte[] content=null;
				try {
					content = str.getBytes("utf-8");
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				BufferedImage bufImg = new BufferedImage(300, 300,
							        BufferedImage.TYPE_INT_RGB);
				
				Graphics2D gs = bufImg.createGraphics(); 
				gs.setBackground(Color.WHITE);
				gs.clearRect(0, 0, 300, 300); 
				gs.setColor(Color.BLACK); 
				if (content.length > 0 && content.length < 120) {
		            boolean[][] codeOut = rcode.calQrcode(content);
		            for (int i = 0; i < codeOut.length; i++) {
		                for (int j = 0; j < codeOut.length; j++) {
		                    if (codeOut[j][i]) {
		                        gs.fillRect(j * 10 + 5, i * 10 + 5, 10, 10);
		                    }
		                }
		            }
		            System.out.println("OK");
		        } else {
		            System.err.println("QRCode content bytes length = " + content.length + " not in [ 0,120 ]. ");
		        }
		        gs.dispose();
		        bufImg.flush();
		        File imgFile = new File("C:\\MESRP\\qr.jpg");
		        try {
		            ImageIO.write(bufImg, "jpg", imgFile);
		        } catch (IOException e) {
		            e.printStackTrace();
		        }
			}
			
			//qrcode hc
			public void PrintQRdj(final String printer) {
				AccessController.doPrivileged(new PrivilegedAction<Object>() {
					@Override
					public Object run() {

						System.out.println("Start2");
						ActiveXComponent axc = new ActiveXComponent("pt1.ct44");
			
							Dispatch dispatch = axc.getObject();
							Variant v = Dispatch.call(dispatch, "ct44",printer);

						return null;
					}
				});
			}
}
